package com.peng.leetcode.dynamic;

/**
 * UniquePathsWithObstacles
 *
 * @author: lupeng6
 * @create: 2020/12/30 10:43
 */
public class UniquePathsWithObstacles {

    public static void main(String[] args) {
        int[][] obstacleGrid = {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}};
        System.out.println(new UniquePathsWithObstacles().uniquePathsWithObstacles(obstacleGrid));
    }

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        if (obstacleGrid == null || obstacleGrid.length == 0) {
            return 0;
        }
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[][] board = new int[m][n];
        for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) {
            board[i][0] = 1;
        }
        for (int i = 0; i < n && obstacleGrid[0][i] == 0; i++) {
            board[0][i] = 1;
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (obstacleGrid[i][j] == 0) {
                    board[i][j] = board[i - 1][j] + board[i][j - 1];
                }
            }
        }
        return board[m - 1][n - 1];
    }

}
